/**
 * Project Name:fire-industry-DM-center
 * File Name:MenuServer.java
 * Package Name:com.firestone.web.center
 * Date:2018年3月28日下午2:06:05
 *
*/

package com.firestone.web.center;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.menu.MenuDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImCenterMenu;
import com.firestone.bean.po.menu.SavePO;
import com.firestone.bean.po.menu.UpdatePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.menu.MenuVO;
import com.firestone.converters.center.ConverterMenu;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireException;
import com.firestone.service.center.IImCenterMenuService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Description: TODO(菜单控制器)
 * @author CAIYJ
 * @date 2018年3月28日 下午2:06:05
 */
@Api(value = "菜单控制器", tags = { "菜单接口文档" })
@RestController
@RequestMapping(value = "/menu")
public class MenuServer extends BaseServer {

    @Autowired
    private IImCenterMenuService imCenterTerminalMenu;

    /**
     * @Description: TODO(菜单列表)
     * @param name
     * @param pagePO
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:56:19
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "列表", notes = "列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "菜单名称", required = false, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<MenuVO>> list(String name,
            @ApiParam PageDTO pagePO) throws Exception {
        Page<MenuDTO> page = new Page<>(pagePO.getPage(), pagePO.getSize());
        page = imCenterTerminalMenu.listMenu(page, name);
        BaseListVO<MenuVO> vo = ConverterMenu.convertListVO(page);
        return ReturnDTO.success(vo);
    }

    /**
     * @Description: TODO(新增菜单)
     * @param po
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:56:28
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/save")
    public ReturnDTO<String> save(@ApiParam SavePO po) throws Exception {
        ImCenterMenu menu = new ImCenterMenu();
        BeanUtils.copyProperties(po, menu);
        User user = getUserInfo();
        menu.setCreator(user.getId());
        boolean result;
        try {
            result = imCenterTerminalMenu.insert(menu);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名菜单", e);
        }
        if (result) {
            return ReturnDTO.success("新增成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(修改菜单)
     * @param id
     * @param po
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:56:35
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/update/{id}")
    public ReturnDTO<String> update(@PathVariable Long id,
            @ApiParam UpdatePO po) throws Exception {
        ImCenterMenu menu = new ImCenterMenu();
        BeanUtils.copyProperties(po, menu);
        User user = getUserInfo();
        menu.setCreator(user.getId());
        menu.setId(id);
        boolean result;
        try {
            result = imCenterTerminalMenu.updateById(menu);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名菜单", e);
        }
        if (result) {
            return ReturnDTO.success("修改成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_update_failed);
    }

    /**
     * @Description: TODO(删除菜单)
     * @param id
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:56:42
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/remove/{id}")
    public ReturnDTO<String> remove(@PathVariable Long id) throws Exception {
        if (imCenterTerminalMenu.deleteById(id)) {
            return ReturnDTO.success("删除成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_delete_failed);
    }
}
